﻿using FairyGUI;

/// <summary>
/// 总结算
/// </summary>
public class UIGameTotalSett : UIPopupWindow {

	class PlayerSettInfo {
		UIHeadpicImage _headpic;
		GTextField _name;
		GTextField _score;
		GTextField _roundScore;
		GImage _winImg;
		GImage _loseImg;

		public PlayerSettInfo(GComponent uiComp) {
			_headpic = new UIHeadpicImage(uiComp.GetChild("头像").asCom);
			_name = uiComp.GetChild("名字").asTextField;
			_score = uiComp.GetChild("总分").asTextField;
			_roundScore = uiComp.GetChild("单局分").asTextField;
			_winImg = uiComp.GetChild("胜利").asImage;
			_loseImg = uiComp.GetChild("失败").asImage;
		}

		public void Set(GameDataKernel.RoundSett.PlayerSett info) {
			_headpic.SetHeadpic(info.headpic);
			_name.text = info.name;
			_score.text = "总分：" + info.score;
			if (info.win) {
				_roundScore.text = "[color=#ff0000]+" + info.roundScore + "[/color]";

				_winImg.visible = true;
				_loseImg.visible = false;
			} else {
				_roundScore.text = "[color=#000000]" + info.roundScore + "[/color]";

				_winImg.visible = false;
				_loseImg.visible = true;
			}
		}
	}

	PlayerSettInfo[] _infos;

	static UIGameTotalSett _inst = null;

	public UIGameTotalSett()
    {
		_inst = this;

        // 加载界面
        base.Init(UIPackage.CreateObject("游戏界面", "总结算").asCom);

        // 初始化退出按钮
		GButton exitBtn = uiComp.GetChild("退出房间按钮").asButton;
		exitBtn.onClick.Add(() => {
			GameProcedure.inst.EnqueueMessage (
				new BaseProceduar.Message(Commands.CMD_MOD_MSG, GameCommands.CMD_EXIT_GAME));
        });

		// 初始化分享按钮
		GButton shareBtn = uiComp.GetChild("分享好友按钮").asButton;
		shareBtn.onClick.Add(() => {
			GameProcedure.inst.EnqueueMessage (
				new BaseProceduar.Message(Commands.CMD_CTRL_BUTTON_CLICKED, GameCommands.CMD_SHARE_GAME_SETT));
		});

		_infos = new PlayerSettInfo[5];
		for (int i = 1; i <= 5; ++i) {
			_infos [i - 1] = new PlayerSettInfo (uiComp.GetChild("玩家" + i).asCom);
		}
			
		foreach (GameDataKernel.RoundSett.PlayerSett playerSett in GameDataKernel.inst.roundSett.setts) {
			int diff = playerSett.index - GameDataKernel.inst.playerIndex;
			if (diff < 0)
			{
				diff += 5;
			}
			_infos [diff].Set (playerSett);
		}
    }

    void _Close()
    {
        DebugInfo.Log("关闭总结算");

        AudioController.inst.PlayUIClickEffect();
        uiComp.RemoveFromParent();
        uiComp.Dispose();
        base.Close();

		_inst = null;
    }

    /// <summary>
    /// 弹出窗口
    /// </summary>
    public static void Popup()
    {
		UIGameLeaveConfirm.CloseWindow ();

		DebugInfo.Log("打开总结算");
		UIRootController.inst.PopupWindow(new UIGameTotalSett());
    }

	/// <summary>
	/// 关闭窗口
	/// </summary>
	public static void CloseWindow() {
		if (_inst != null) {
			_inst._Close ();
		}
	}
}
